<template>
	<view class="login">
		<view class="login-title">
			<text>登录</text>
		</view>
		<view class="login-form">
			<uni-forms ref="form" :modelValue="form" label-position="top" :rules="rules">
				<uni-forms-item label=" " name="login_id">
					<uni-easyinput startFix="temp" type="text" v-model="form.login_id" placeholder="手机号码" />
				</uni-forms-item>
				<uni-forms-item label=" " name="login_pass">
					<uni-easyinput startFix="temp" type="password" v-model="form.login_pass" placeholder="密码" />
				</uni-forms-item>
			</uni-forms>
			<view class="regist">
				<text @click="invokeReg">立即注册</text>
			</view>
		</view>
		<view class="login-btn">
			<button type="default" @click="handleLogin">登录</button>
		</view>
	</view>
</template>

<script>
	import {doLogin} from '@/api/login.js'
	import {getLoginUserDetail} from '@/api/user.js'
	import {setAccessToken,setBaseUser} from '@/util/cache.js'
	export default {
		components: {
		},
		data() {
			return {
				rules: {
					// 对name字段进行必填验证
					login_id: {
						rules: [{
								required: true,
								errorMessage: '请输入手机号',
							},
							{
								minLength: 11,
								maxLength: 11,
								errorMessage: '手机号长度在 {minLength} 到 {maxLength} 个字符',
							}
						]
					},
					login_pass: {
						rules: [{
								required: true,
								errorMessage: '请输入密码',
							},
							{
								minLength: 6,
								maxLength: 20,
								errorMessage: '密码长度在 {minLength} 到 {maxLength} 个字符',
							}
						]
					},
				},
				form: {
					login_id: '13078801215',
					login_pass: '123456',
					platform: 'wap'
				}
			}
		},
		mounted() {
		},
		methods: {
			handleLogin() {
				this.$refs.form.validate().then(res=>{
					uni.showLoading({
						title: '登录中...',
						mask:true
					});
					let result = doLogin({...res,platform:'wap'})
					result.then(r => {
						if(r.code == 0) {
							// 登录成功
							setAccessToken(r.data.token)
							// 提示错误
							uni.showToast({
								icon: "success",
								title: "登录成功"
							})
							// 获取登录用户信息
							getLoginUserDetail({uid:r.data.user.user_id}).then(x => {
								if(x.code == 0) {
									// 登录成功
									setBaseUser(x.data)
									var pages = getCurrentPages(); // 当前页面
									if(pages && pages.length > 1) {
										var beforePage = pages[pages.length - 2]; // 前一个页面
										uni.navigateBack({
											 success: function() {
												 beforePage.onLoad(); // 执行前一个页面的onLoad方法
											 }
										})
									} else {
										uni.switchTab({
											url: '/pages/user/user'
										})
									}
								}
							})
						}
					})
				}).catch(err =>{
					console.log('表单错误信息：', err);
				})
			},
			invokeReg() {
				// 跳转注册
				uni.navigateTo({
					url:'/pages/user/regist'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.login {
		padding: 0 40rpx 40rpx 40rpx;
		.login-title {
			height: 120rpx;
			line-height: 120rpx;
			font-weight: 600;
			font-size: 36rpx;
		}
		.login-form {
			margin-top: 80rpx;
			.login-password {
				margin-top: 40rpx;
			}
			.regist {
				height: 60rpx;
				line-height: 60rpx;
				text-align: right;
			}
		}
		.login-btn {
			margin-top: 80rpx;
			button {
				width: 100%;
				border-radius: 0;
				background-color: black;
				color: #ffff;
				font-size: 30rpx;
				height: 80rpx;
				line-height: 80rpx;
			}
		}
	}
</style>
